package com.lgf.view.panel;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.plaf.metal.MetalIconFactory;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.lgf.entity.CodeFileVo;
import com.lgf.tool.cache.CacheHelper;
import com.lgf.tool.file.CodeFileHelper;
import com.lgf.view.IView;
import com.lgf.view.listener.RunBtnClickListener;
import com.lgf.vo.CodeFileTypeEnum;
import com.lgf.vo.Constants;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 代码
 * 
 * @author zhouz
 * @date 2019年11月13日
 *
 */
@Slf4j
public class CodeNotesPanel implements IView<JPanel> {
	private Set<String> fileTypes = new HashSet<String>();
	private static String[] columnNames = { "序号", "文件名", "文件类型", "目录" };

	private List<CodeFileVo> selectFiles = null;

	private JTable infoTable;

	private JLabel runProgressLabel;// 进度
	private JLabel runStatusLabel;// 运行状态
	private RunBtnClickListener runBntListener;// 运行按钮监听

	@Override
	public JPanel layout() {
		JPanel container = new JPanel();
		BorderLayout layout = new BorderLayout(1, 1);
		container.setLayout(layout);
		JPanel titlePanel = new JPanel();
		container.add(titlePanel, BorderLayout.NORTH);
		this.layoutTitlePanel(titlePanel);

		JPanel bottomPanel = new JPanel();
		container.add(bottomPanel, BorderLayout.SOUTH);
		this.layoutBottomPanel(bottomPanel);

		JPanel bodyPanel = new JPanel();
		container.add(bodyPanel, BorderLayout.CENTER);
		this.layoutBodyPanel(bodyPanel);

		return container;
	}

	/**
	 * 底部状态栏
	 * 
	 * @author zhouz
	 * @date 2019年11月21日
	 * 
	 * @param bottomPanel
	 */
	private void layoutBottomPanel(JPanel bottomPanel) {
		BorderLayout layout = new BorderLayout();
		bottomPanel.setLayout(layout);
		this.runStatusLabel = new JLabel();
		this.runProgressLabel = new JLabel();

		bottomPanel.add(this.runStatusLabel, BorderLayout.EAST);
		bottomPanel.add(this.runProgressLabel, BorderLayout.WEST);
	}

	/**
	 * 处理中部内容操作
	 * 
	 * @author zhouz
	 * @date 2019年11月14日
	 * 
	 * @param bodyPanel
	 */
	private void layoutBodyPanel(JPanel bodyPanel) {
		BorderLayout layout = new BorderLayout();
		bodyPanel.setLayout(layout);

		this.infoTable = new JTable();
		this.infoTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		for (String cname : columnNames) {
			TableColumn column = new TableColumn();
			column.setHeaderValue(cname);
			this.infoTable.addColumn(column);
		}
		bodyPanel.add(this.infoTable, BorderLayout.CENTER);
		this.initBodyTables(new ArrayList<CodeFileVo>());
	}

	private void initBodyTables(List<CodeFileVo> files) {
		this.runBntListener.setFiles(files);
		log.info("更新了" + files.size() + "条数据");
		this.runProgressLabel.setText("共" + files.size() + "个文件");
		this.runStatusLabel.setText("未开始");
		int i = 0;
		Object[][] rows = new Object[files.size()][];
		for (CodeFileVo file : files) {
			Object[] row = new Object[columnNames.length];
			row[0] = (i + "");
			row[1] = (file.getFile().getName());
			row[2] = (file.getType().getText());
			row[3] = (file.getFile().getPath());
			rows[i++] = row;
		}
		this.infoTable.setEnabled(false);
		DefaultTableModel model = (DefaultTableModel) this.infoTable.getModel();
		model.setDataVector(rows, columnNames);
	}

	/**
	 * 处理头部操作
	 * 
	 * @author zhouz
	 * @date 2019年11月14日
	 * 
	 * @param titlePanel
	 */
	private void layoutTitlePanel(JPanel titlePanel) {
		FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
		titlePanel.setLayout(flowLayout);
		titlePanel.add(new JLabel("选择要处理的文件"));
		JButton selectFileBtn = new JButton(MetalIconFactory.getFileChooserListViewIcon());
		titlePanel.add(selectFileBtn);
		selectFileBtn.setText("选择");
		selectFileBtn.addActionListener(e -> {
			JFileChooser fc = new JFileChooser();
			String selDirPath = CacheHelper.get(Constants.Cache.TYPE_FILE, Constants.Cache.KEY_SELECT_DIR);
			if (StrUtil.isNotEmpty(selDirPath)) {
				fc.setCurrentDirectory(new File(selDirPath));
			}
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fc.setMultiSelectionEnabled(false);
			fc.showDialog(new Label(), "选择");
			File file = fc.getSelectedFile();
			runBntListener.setBaseDir(file);
			File df = file.getParentFile();
			CacheHelper.put(Constants.Cache.TYPE_FILE, Constants.Cache.KEY_SELECT_DIR, df.getPath());
			selectFiles = CodeFileHelper.checkCodeFile(CodeFileTypeEnum.getTypes(), file);
			this.initBodyTables(selectFiles);
		});

		FileTypeCheckBoxClickListener typeCheckBoxListener = new FileTypeCheckBoxClickListener();
		JPanel typeSelectPanel = new JPanel();
		typeSelectPanel.setBorder(BorderFactory.createEtchedBorder());
		titlePanel.add(typeSelectPanel);
		this.addTypeCheckBox(typeSelectPanel, CodeFileTypeEnum.PHP, typeCheckBoxListener);
		this.addTypeCheckBox(typeSelectPanel, CodeFileTypeEnum.HTML, typeCheckBoxListener);
		this.addTypeCheckBox(typeSelectPanel, CodeFileTypeEnum.JS, typeCheckBoxListener);
		this.addTypeCheckBox(typeSelectPanel, CodeFileTypeEnum.CSS, typeCheckBoxListener);

		titlePanel.add(new JLabel("输出路径"));
		JButton selectOutFileBtn = new JButton(MetalIconFactory.getFileChooserListViewIcon());
		titlePanel.add(selectOutFileBtn);
		selectOutFileBtn.setText("选择");

		JButton runBtn = new JButton("运行");
		this.runBntListener = new RunBtnClickListener();
		runBtn.addActionListener(this.runBntListener);
		runBtn.setEnabled(false);
		titlePanel.add(runBtn);
		titlePanel.setBorder(BorderFactory.createEtchedBorder());

		selectOutFileBtn.addActionListener(e -> {
			JFileChooser fc = new JFileChooser();
			String selDirPath = CacheHelper.get(Constants.Cache.TYPE_FILE, Constants.Cache.KEY_SELECT_OUT_DIR);
			if (StrUtil.isNotEmpty(selDirPath)) {
				fc.setCurrentDirectory(new File(selDirPath));
			}
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fc.setMultiSelectionEnabled(false);
			fc.showDialog(new Label(), "选择");
			File outDir = fc.getSelectedFile();
			File df = outDir.getParentFile();
			CacheHelper.put(Constants.Cache.TYPE_FILE, Constants.Cache.KEY_SELECT_OUT_DIR, df.getPath());
			runBntListener.setOutDir(outDir);
			runBtn.setEnabled(true);
		});

	}

	private void addTypeCheckBox(JPanel typeSelectPanel, CodeFileTypeEnum type,
			FileTypeCheckBoxClickListener typeCheckBoxListener) {
		JCheckBox box = new JCheckBox(type.getText());
		box.addActionListener(typeCheckBoxListener);
		typeSelectPanel.add(box);
	}

	private class FileTypeCheckBoxClickListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String type = e.getActionCommand();
			if (fileTypes.contains(type)) {
				fileTypes.remove(type);
			} else {
				fileTypes.add(type);
			}
			runBntListener.setTypes(fileTypes);
		}
	}

}
